import router from "./index.js";
import { ElMessage } from "element-plus";
import useUserStroe from "@/stores/user.js";

const whiteList = ["/login", "/home","/introduction/*", "/404"];
const loginBlackList = ["/login"];
router.beforeEach((to, from, next) => {
  console.log(to, from)
  const userStore = useUserStroe();
  // 校验登录
  if (userStore.user?.token) {
    // 如果登录了,禁止回到
    if (loginBlackList.includes(to.path)) {
      next(from.fullPath);
      return;
    }
    next();
  } else {
    // 未登录
    // 如果是白名单内跳转不校验
    const f = whiteList.some(s => {
      const e = new RegExp(`${s}.*$`).test(to.path)
      console.log(s, e, "11")
      return e
    })
    if (f) {
      next();
      return;
    }
    ElMessage.error("您还未登录,请先登录");
    next(`/login?redirect=${from.fullPath}`);
  }
});
